const { validateField } = require('../../helpers/validator');
const Address = require('../../models/user/address');
const Koa = require('koa');
const db = require('../../helpers/db');

/**
 * 添加收货地址
 * @param {Koa} ctx Koa上下文
 */
async function addAddress(ctx) {
    const {
        contact = '',
        phone = '',
        province = '',
        city = '',
        county = '',
        detailAddress = '',
        remark = ''
    } = ctx.request.body;
    const { id } = ctx.state.user;
    try {
        // 参数校验
        validateField({ contact, phone, province, city, county, detailAddress });
        await Address.create({
            user: id,
            contact,
            phone,
            province,
            city,
            county,
            detailAddress,
            remark
        });
        ctx.status = 201;
    } catch (error) {
        ctx.throw(error.status || 500, error);
    }
}

/**
 * 编辑收货地址
 * @param {Koa} ctx Koa上下文
 */
async function patchAddress(ctx) {
    const {
        id = '',
        contact = '',
        phone = '',
        province = '',
        city = '',
        county = '',
        detailAddress = '',
        remark = ''
    } = ctx.request.body;
    const { id: user } = ctx.state.user;
    try {
        // 参数校验
        validateField({ id, contact, phone, province, city, county, detailAddress });
        const count = await Address.update(
            {
                contact,
                phone,
                province,
                city,
                county,
                detailAddress,
                remark
            },
            {
                where: {
                    id,
                    user
                }
            }
        );
        if (count === 0) {
            throw new Error('更新失败');
        }
        ctx.status = 205;
    } catch (error) {
        ctx.throw(error.status || 500, error);
    }
}

/**
 * 删除收货地址
 * @param {Koa} ctx Koa上下文
 */
async function deleteAddress(ctx) {
    const { id = '' } = ctx.request.path;
    const { id: user } = ctx.state.user;
    // 参数校验
    validateField({ id });
    const count = await Address.destroy({
        where: {
            id,
            user
        }
    });
    if (count === 0) {
        throw new Error('删除失败');
    }
    ctx.status = 204;
}

/**
 * 获取收货地址列表
 * @param {Koa} ctx Koa上下文
 */
async function queryAddressList(ctx) {
    const { id: user } = ctx.state.user;
    const result = await Address.findAll({
        where: {
            user
        }
    });
    ctx.status = 200;
    ctx.body = result;
}

/**
 * 获取收货地址列表
 * @param {Koa} ctx Koa上下文
 */
async function setDefaultAddr(ctx) {
    const { id: user } = ctx.state.user;
    const { id } = ctx.request.path;
    const transaction = await db.transaction();
    try {
        await Address.update(
            {
                def: false
            },
            {
                where: {
                    user
                },
                transaction
            }
        );
        const count = await Address.update(
            {
                def: true
            },
            {
                where: {
                    id,
                    user
                },
                transaction
            }
        );
        await transaction.commit();
        if (count === 0) {
            throw new Error('设置失败');
        }
        ctx.status = 205;
    } catch (error) {
        transaction.rollback();
        throw error;
    }
}

module.exports = {
    addAddress,
    patchAddress,
    deleteAddress,
    queryAddressList,
    setDefaultAddr
};
